<template>
  <div class='category'>
    <el-card>
      <el-form inline label-width="100px">
        <el-form-item label="一级分类">
          <el-select v-model="value1" placeholder="请选择" size="small" @change="changeCategory1Id"
            :disabled="disabled !== 1">
            <el-option v-for="item in category1List" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级分类">
          <el-select v-model="value2" placeholder="请选择" size="small" @change="changeCategory2Id"
            :disabled="disabled !== 1">
            <el-option v-for="item in category2List" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="三级分类">
          <el-select v-model="value3" placeholder="请选择" size="small" @change="changeCategory3Id"
            :disabled="disabled !== 1">
            <el-option v-for="item in category3List" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { mapActions, mapState, mapMutations } from 'vuex'
export default {
  // 302
  name: 'Category',
  data() {
    return {
      value1: '',
      value2: '',
      value3: '',
    }
  },
  props: ['disabled'],
  mounted() {
    this.getCategory1()
  },
  methods: {
    ...mapActions('category', ['getCategory1', 'getCategory2', 'getCategory3']),
    // 点击一级分类的回调
    changeCategory1Id() {
      this.SET_CATEGORY1ID(this.value1)
      this.value2 = ''
      this.value3 = ''
      this.getCategory2()
    },
    // 点击二级分类的回调
    changeCategory2Id(id) {
      this.value3 = ''
      this.SET_CATEGORY2ID(this.value2)
      this.getCategory3()
    },
    // 点击三级分类的回调
    changeCategory3Id() {
      this.SET_CATEGORY3ID(this.value3)
    },
    ...mapMutations('category', ['SET_CATEGORY1ID', 'SET_CATEGORY2ID', 'SET_CATEGORY3ID'])
  },
  computed: {
    ...mapState('category', ['category1List', 'category2List', 'category3List', 'category1Id', 'category2Id', 'category3Id']),
  },
  watch: {
    $route: {
      handler() {
        this.value1 = this.category1Id;
        this.value2 = this.category2Id;
        this.value3 = this.category3Id;
      },
      deep: true,
      immediate: true
    }
  }
}
</script>

<style></style>